home *** CD-ROM | disk | FTP | other *** search
- Path: news.iag.net!news
- From: jatmon@iag.net (John R Buchan)
- Newsgroups: comp.lang.c
- Subject: Re: using MALLOC() w/ 2-d arrays
- Date: 28 Jan 1996 17:49:13 GMT
- Organization: Internet Access Group, Orlando, Florida
- Message-ID: <4egcup$d6r@news.iag.net>
- References: <4e9nm2$nna@cville-srv.wam.umd.edu>
- NNTP-Posting-Host: pm2-orl19.iag.net
- X-Newsreader: WinVN 0.99.7
-
- In article <4e9nm2$nna@cville-srv.wam.umd.edu>, jsquires@wam.umd.edu says...
- ->
- ->I have a function that takes as one of its arguments:
- ->
- ->unsigned char image[MAX_Y][MAX_X}
- ->
- ->
- ->I have a number of images which is determined at run time,
- ->therefore I can't declare the size of image_array[][MAX_Y][MAX_X].
- ->
- ->In other words, I want to be able to allocate space for as many
- ->of these images as I need at run time. I've tried everything
- ->(except the correct way).
- ->
- ->I think I need something of the form:
- ->
- ->unsigned char * image_array[MAX_Y][MAX_X];
- ->
- ->image_array = (unsigned char *) malloc(.....sizeof(unsigned char));
- ->
- ->
- ->I know that if I declare:
- ->unsigned char image_array[10][MAX_Y][MAX_X];
- ->
- ->I can successfully send this to the function, but what
- ->if I don't know that I'll need ten? Can anyone tell
- ->me the proper solution? Thank you.
- ->
- ->
-
- The c.l.c faq (Frequently Asked Question) list has some explanations on the
- allocation and passing of multidimensional arrays. You might want to read
- through it. It is available for anonymous ftp from rtfm.mit.edu
- /pub/usenet/comp.lang.c.
-
- Probably the most readable solution would be to create a typedef for
- image. Maybe:
-
- typedef unsigned char image[MAX_Y][MAX_X];
-
- Then you would use image like any other type;
-
- image imageObj, imageAry[8], *imagePtr;
-
- imageObj[1][3] = 4;
- imagePtr = malloc( 5 * sizeof(image)); /* array of 5 images */
-
- int ImageFunc( int imageCnt, image *ary); /* declare */
- ImageFunc( 5, imagePtr); /* call */
- ImageFunc( 8, imageAry);
-
- --
- John R Buchan -:|:- Looking for that elusive FAQ? ftp to:
- jatmon@mail.iag.net -:|:- rtfm.mit.edu /pub/usenet-by-group/....
-
-